package htw.list;

// Java-Beispiele
// Prof. Dr. H. G. Folz
// 2001
//
 
import java.io.*;
import java.util.*;
import de.htw.saarland.stl.Stdin;

/** Test der ListenKlasse
 */
public class ListTest {
    private final int ADD       = 1;
    private final int ADD_FIRST = 2;
    private final int ADD_LAST  = 3;
    private final int CLEAR     = 4;
    private final int CONTAINS  = 5;
    private final int GET       = 6;
    private final int GET_FIRST = 7;
    private final int GET_LAST  = 8;
    private final int INDEX_OF  = 9;
    private final int REMOVE    = 10;
    private final int REMOVE_INDEX = 11;
    private final int REMOVE_FIRST = 12;
    private final int REMOVE_LAST = 13;
    private final int SET       = 14;
    private final int SIZE      = 15;
    private final int ITERATOR  = 16;
    private final int ENDE      = 99;

    private PrintStream out = System.out;

    public ListTest() {
    }

    public void start() {
        int auswahl = Stdin.readlnInt("0: List oder 1: DList");
        AbstractList l1;
        if (auswahl == 0)
            l1 = new List();
        else
            l1 = new DList();
        int index = 0;
        String eingabe;
        do {
           try {
              out.println("\nl1: " + l1 + "\n");
              auswahl = auswaehlen();
              out.println("\n");
              switch (auswahl) {
              case ADD:
                        index   = Stdin.readlnInt   ("add: Index  = ");
                        eingabe = Stdin.readlnString("add: Inhalt = ");
                        l1.add(index, eingabe);
                        break;
              case ADD_FIRST:
                        eingabe = Stdin.readlnString("addFirst: Inhalt = ");
                        l1.addFirst(eingabe);
                        break;
              case ADD_LAST:
                        eingabe = Stdin.readlnString("addLast: Inhalt = ");
                        l1.addLast(eingabe);
                        break;
              case CLEAR:
                        out.println("clear: Liste l�schen");
                        l1.clear();
                        break;
              case CONTAINS:
                        eingabe = Stdin.readlnString("contains: Inhalt = ");
                        if (l1.contains(eingabe))
                             out.println(eingabe + " in der Liste vorhanden");
                        else
                             out.println(eingabe + " nicht in der Liste");
                        break;
              case GET:
                        index   = Stdin.readlnInt   ("get: Index  = ");
                        out.println("get("+index+") = " + l1.get(index));
                        break;
              case GET_FIRST:
                        out.println("getFirst: " + l1.getFirst());
                        break;
              case GET_LAST:
                        out.println("getLast: " + l1.getLast());
                        break;
              case INDEX_OF:
                        eingabe = Stdin.readlnString("indexOf: Inhalt = ");
                        out.println("indexOf("+eingabe+") = "
                                + l1.indexOf(eingabe));
                        break;
              case REMOVE:
                        eingabe = Stdin.readlnString("remove: Inhalt = ");
                        l1.remove(eingabe);
                        break;
              case REMOVE_INDEX:
                        index = Stdin.readlnInt("remove: index = ");
                        l1.remove(index);
                        break;
              case REMOVE_FIRST:
                        out.println("removeFirst: ");
                        l1.removeFirst();
                        break;
              case REMOVE_LAST:
                        out.println("removeLast: ");
                        l1.removeLast();
                        break;
              case SET:
                        index   = Stdin.readlnInt   ("set: Index  = ");
                        eingabe = Stdin.readlnString("set: Inhalt = ");
                        l1.set(index, eingabe);
                        break;
              case SIZE:
                        out.println("size: " + l1.size());
                        break;
              case ITERATOR:
                        out.println("iterator: ");
//                        Iterator it = l1.iterator();
                        int i = 0;
//                         while (it.hasNext())
//                             out.println(i++ + ": " + it.next());
                        for (Object obj : l1)
                             out.println(i++ + ": " + obj);
                        break;
              case ENDE : break;
              default   : System.out.println("Falsche Auswahl!");
              }
           } catch (NoSuchElementException e) {
               out.println(e);
           } catch (Exception e) {
               e.printStackTrace();
           }
        } while (auswahl != ENDE);

    }

    public int auswaehlen() {
        out.print(
                        + ADD  + ": add / "
                        + ADD_FIRST + ": addFirst / "
                        + ADD_LAST  + ": addLast / "
                        + CLEAR + ": clear / "
                        + CONTAINS + ": contains / "
                        + GET + ": get /\n"
                        + GET_FIRST + ": getFirst  / "
                        + GET_LAST + ": getLast / "
                        + INDEX_OF + ": indexOf / "
                        + REMOVE + ": remove / "
                        + REMOVE_INDEX + ": remove(index) / "
                        + REMOVE_FIRST + ": removeFirst /\n"
                        + REMOVE_LAST + ": removeLast / "
                        + SET + ": set / "
                        + SIZE + ": size / "
                        + ITERATOR + ": iterator / "
                        + ENDE  + ": beenden -> ");
       return Stdin.readlnInt();
   }


    public static void main(String[] args) {
        try {
            ListTest l = new ListTest();
            l.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
